from datetime import datetime

from elasticsearch_dsl import (
    Document,
    InnerDoc,
    Short,
    Keyword,
    Long,
    Float,
    Nested,
    Text,
    Float,
    Date,
    Join,
    Boolean,
    connections,
    analyzer,
)

from config import settings
from core.es import Index_KJJ_ALUMNI

connections.create_connection(hosts=[settings.dsl], timeout=80)


class Member(Document):
    name_zh = Text(fields={"keyword": Keyword()}, analyzer="ik_max_word", position_increment_gap=100)
    # name_pinyin = Text(store=False, term_vector="with_offsets", analyzer="pinyin_analyzer", boost=10)
    name_pinyin = Text()
    identify_type = Text()
    id = Text()
    person_id = Text()
    gender = Text(fields={"keyword": Keyword()})
    age = Short()
    nation = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word", position_increment_gap=100)

    # 职称
    professional_titles = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word",
                               position_increment_gap=100)
    # 职务
    job_title = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word", position_increment_gap=100)
    research_interests = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word",
                              position_increment_gap=100)
    academic_area = Text()
    academic_qualification = Text()  # 最高学历
    job_province = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word",
                        position_increment_gap=100)
    job_city = Text()
    subject = Keyword()
    role = Text()
    nationality = Text()
    email = Keyword()
    cur_institution_name = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word",
                                position_increment_gap=100)

    cur_institution_department = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word",
                                      position_increment_gap=100)
    num_of_activities = Long()
    number_of_applicants = Long()
    identify_photo = Text()
    political_status = Text()
    graduate_school = Text()  # 毕业学习
    degree = Text()  # 学历  # 博士 学历
    academic_qualification = Text()  # 博士 学历
    created_at = Date(required=True)
    update_at = Date(required=True)
    active = Float()  # 活跃度
    activation = Long()
    region = Text(fields={"keyword": Keyword(ignore_above=256)}, analyzer="ik_max_word", position_increment_gap=100)
    prank_alumni_score = Float()  # 排名1
    rank_alumni_score = Float()  # 排名2

    belongs_inst = Keyword()  # 人隶属的部门

    class Index:
        name = Index_KJJ_ALUMNI
        settings = {
            "number_of_shards": 1,
            "number_of_replicas": 0,
        }

    def save(self, **kwargs):
        # if there is no date, use now
        if self.created_at is None:
            self.created_at = datetime.now()
        return super().save(**kwargs)


def setup():
    """Create an IndexTemplate and save it into elasticsearch."""
    index_template = Member._index.as_template("base")
    index_template.save()


if __name__ == "__main__":
    # initiate the default connection to elasticsearch
    connections.create_connection(hosts=[settings.dsl], timeout=120)

    # create index
    setup()
    Member.init()
